package edu.controller.converter;

import java.util.Map;
import java.util.TreeMap;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import edu.terapia.model.Persona;


@FacesConverter(value="personaConverter", forClass=Persona.class)
public class PersonaConverter implements Converter {

	private static final Map<Integer, Persona> oResource = new TreeMap<Integer, Persona>(); 
	
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String idValue) {
		Persona selected = new Persona();
		Integer id = null;
		try {
			id = new Integer(idValue);
			if (oResource.containsKey(id)) {
				selected = oResource.get(id);
			}
		} catch (Exception ex) {
		}
		return selected;
	}

	public String getAsString(FacesContext arg0, UIComponent arg1, Object tipo) {
		if(tipo == null  || "".equals(tipo)) {
			return "";
		} else {
			oResource.put( ((Persona)tipo).getId(), (Persona)tipo );
			return  String.valueOf( ((Persona)tipo).getId() );
		}
	}

}
